import { ref, toRaw, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { cloneDeep, omit } from "lodash-es";
import { useTabbarStore } from "./useTabbarStore.mjs";
import { useTabbarPathStore } from "./useTabbarPathStore.mjs";
function useTabbar() {
  const route = useRoute();
  const router = useRouter();
  const tabbarStore = useTabbarStore();
  const { addPath, closePath, updatePath, tabbarPathList, hasPath } = useTabbarPathStore();
  const tabbars = ref(tabbarStore.tabbars.value);
  const clickTab = (tab) => {
    router.push(tab.fullPath);
  };
  const closeTab = (tab) => {
    closePath(tab.fullPath);
    return tabbarStore.closeTab(tab);
  };
  const getActiveTab = (key) => {
    return tabbars.value.find((tab) => tab.fullPath === key);
  };
  const closeLeftAllTabs = (fullPath) => {
    const cloneTabs = cloneDeep(toRaw(tabbars.value));
    const index = cloneTabs.findIndex((tab) => tab.fullPath === fullPath);
    if (index >= 1) {
      for (let i = 0; i < index; i++) {
        const tab = cloneTabs[i];
        closeTab(tab);
      }
    }
  };
  const closeRightAllTabs = (fullPath) => {
    const cloneTabs = cloneDeep(toRaw(tabbars.value));
    const index = cloneTabs.findIndex((tab) => tab.fullPath === fullPath);
    if (index < tabbars.value.length - 1) {
      for (let i = index + 1; i < cloneTabs.length; i++) {
        const tab = cloneTabs[i];
        closeTab(tab);
      }
    }
  };
  const closeAllTabs = () => {
    const cloneTabs = cloneDeep(toRaw(tabbars.value));
    for (let i = 0; i < cloneTabs.length; i++) {
      const tab = cloneTabs[i];
      closeTab(tab);
    }
  };
  const updateTabMeta = (fullPath, meta) => {
    const tab = tabbars.value.find((tab2) => tab2.fullPath === fullPath);
    tab.meta = {
      ...tab == null ? void 0 : tab.meta,
      ...meta
    };
  };
  const closeAndOpenNextTab = (tab) => {
    if (!tab) {
      return;
    }
    closeTab(tab);
    if (tabbarPathList.value.length > 0) {
      router.push(tabbarPathList.value[tabbarPathList.value.length - 1]);
    } else {
      router.push("/");
    }
  };
  watch(() => route.fullPath, () => {
    var _a, _b, _c;
    if (route.fullPath === "/") {
      return;
    }
    let meta = (_b = (_a = route.matched) == null ? void 0 : _a[route.matched.length - 1]) == null ? void 0 : _b.meta;
    const cloneRoute = cloneDeep(toRaw(omit(route, ["matched", "redirectedFrom"])));
    const title = (_c = route.query) == null ? void 0 : _c.title;
    if (title) {
      meta = {
        ...meta,
        title
      };
    }
    tabbarStore.addTab({
      ...cloneRoute,
      meta: meta || route.meta
    });
    if (hasPath(route.fullPath)) {
      updatePath(route.fullPath);
    } else {
      addPath(route.fullPath);
    }
    ;
  }, { immediate: true });
  return {
    clickTab,
    closeTab,
    closeAllTabs,
    closeLeftAllTabs,
    closeRightAllTabs,
    closeAndOpenNextTab,
    getActiveTab,
    updateTabMeta,
    tabbars
  };
}
export {
  useTabbar
};
//# sourceMappingURL=useTabbar.mjs.map
